/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.arrye.Flickr;

import javax.microedition.lcdui.*;
import javax.microedition.m3g.Graphics3D;

/**
 * @author Wilson.ARG
 */
public class ImageList extends Canvas {
    private Image[] _images;
    private int _imageWidth, _imageHeight, _screenHeight, _screenWidth, _current;
    /**
     * constructor
     */
    public ImageList() {
        try {
	    // Add the Exit command
            _current = 0;
            _screenHeight = getHeight();
            _screenWidth = getWidth();
        } catch(Exception e) {
            e.printStackTrace();
        }
    } 
    
    public void Initialize(Image[] images, int imageWidth, int imageHeight, String title) {
        _images = images;
        _imageWidth = imageWidth;
        _imageHeight = imageHeight;
        this.setTitle(title);
    }
    
    /**
     * paint
     */
    public void paint(Graphics g) {
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, _screenWidth, _screenHeight);
        int x = 0, y = 0;
        for (int i = 0; i < _images.length; i++) {
            g.drawImage(_images[i], x+1, y+1, Graphics.LEFT | Graphics.TOP);
            x += _imageWidth+2;
            if (x+_imageWidth+2 > _screenWidth) {
                x = 0;
                y += _imageHeight+2;
            }
        }
        g.setColor(255, 255, 255);
        int cx = (_current%3)*(_imageWidth+2);
        int cy = ((int)(_current/3))*(_imageHeight+2);
        g.drawRect(cx, cy, _imageWidth+1, _imageHeight+1);
        g.drawRect(cx+1, cy+1, _imageWidth, _imageHeight);
    }
    
    /**
     * Called when a key is pressed.
     */
    protected  void keyPressed(int keyCode) {
        int key = getGameAction(keyCode);
        switch (key) {
            case DOWN: {
                    if (_current+3 < _images.length)
                        _current += 3;
                    else _current = 0;
                    repaint();
                }
                break;
            case UP: {
                    if (_current-3 >= 0)
                        _current -= 3;
                    else _current = _images.length-1;
                    repaint();
                }
                break;
            case LEFT: {
                    if (_current-1 >= 0)
                        _current--;
                    else _current = _images.length-1;
                    repaint();
                }
                break;
            case RIGHT: {
                    if (_current+1 < _images.length)
                        _current++;
                    else _current = 0;
                    repaint();
                }
                break;
        }
    }
    
    public int getCurrent() {
        return _current;
    }
    
}